package com.xxx.common.web.configure;

import com.xxx.common.base.constants.MdcKey;
import feign.RequestInterceptor;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {

    @Bean
    public RequestInterceptor authRequestInterceptor() {
        return template -> {
            // 1. 传递 MDC 中的 traceId
            String traceId = MDC.get(MdcKey.TRACE_ID);
            if (traceId != null) {
                template.header(MdcKey.TRACE_ID, traceId);
            }

            // 1. 传递 MDC 中的 token
            String userToken = MDC.get(MdcKey.USER_TOKEN);
            if (userToken != null) {
                template.header(MdcKey.USER_TOKEN, userToken);
            }

            // 1. 传递 MDC 中的 tenantID
            String tenantId = MDC.get(MdcKey.TENANT_ID);
            if (tenantId != null) {
                template.header(MdcKey.TENANT_ID, tenantId);
            }

        };
    }

}